<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <script src="../script/api.js"></script>
    <style>

    </style>
</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            设备点检
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:0px;">
        <ul class="aui-list aui-form-list" style="font-size: 14px;">

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width:30%;font-size: 14px;">设备</div>
                    <div class="aui-list-item-input" style="float:left;width: 70%;">
                        <input type="text" id="name" placeholder="设备名称/编码">
                    </div>
                    <div class="aui-list-item-label1" style="margin-top:5px;font-size: 14px;" onclick="doScan('name');">
                        <img src="../image/scanner.png" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width:30%;font-size: 14px;">开始日期</div>
                    <div class="aui-list-item-input" style="float:left;width: 70%;">
                        <input type="text" id="actualBeginDate" onclick="showTime('actualBeginDate');">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width:30%;font-size: 14px;">结束日期</div>
                    <div class="aui-list-item-input" style="float:left;width: 70%;">
                        <input type="text" id="actualEndDate" onclick="showTime('actualEndDate');">
                    </div>
                </div>
            </li>
        </ul>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="doSearch()">搜索</div>
        <!-- <div class="aui-btn aui-btn-primary" style="margin-right: 0.5rem;background-color: #03a9f4" onclick="doSearch()">搜索</div> -->
        <!-- </div> -->
        <ul class="ui-selectee" id="tbodyHtml">

        </ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script>
    var list;
    apiready = function () {
        $("#name").focus();
        idSelector = "name";
        var query = api.pageParam.query;
        userFlag = query.userFlag;
        // 使用示例
        var weekRange = getCurrentWeekRange();
        $("#actualBeginDate").val( weekRange.monday);
        $("#actualEndDate").val( weekRange.sunday);
        doSearch();
    }

    //扫描
    function doScan(id) {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            $("#" + id).val(ret);
        });
    }

    function getCurrentWeekRange() {
        // 获取当前日期
        var today = new Date();
        // 获取今天是本周的第几天（0是周日，1是周一，...，6是周六）
        var day = today.getDay() || 7; // 调整为1-7表示周一到周日

        // 计算周一的日期（当前日期减去day-1天）
        var monday = new Date(today);
        monday.setDate(today.getDate() - (day - 1));

        // 计算周日的日期（当前日期加上7-day天）
        var sunday = new Date(today);
        sunday.setDate(today.getDate() + (7 - day));
        var mm = {
            monday: formatDate(monday),
            sunday: formatDate(sunday)
        };
        var mondayStr = formatDate(monday);
        var sundayStr = formatDate(sunday);

        
        // 返回格式化后的日期对象
        return {
            monday: mondayStr,
            sunday: sundayStr
        };
    }

    // 辅助函数：格式化日期为YYYY-MM-DD格式
    function formatDate(date) {
        var year = date.getFullYear();
        var month = String(date.getMonth() + 1).padStart(2, '0');
        var day = String(date.getDate()).padStart(2, '0');
        var result = `${year}-${month}-${day}`;
        return result;
    }


    function showTime(id) {
        var moduleTimer = api.require('moduleTimer');
        moduleTimer.showTimer({
            startTime: '2000-01-1 00:00',//可选择的起始时间
            endTime: '2099-12-31 23:59',//可选择的结束时间
            timeFormat: 'yyyy-MM-dd',//返回的时间格式，如果不传默认是yyyy-MM-dd HH:mm:ss
            selColor: '#079962'//选中的颜色值
        }, function (ret, err) {
            var actualBeginDate = ret;
            var text = document.getElementById(id);
            text.value = actualBeginDate;
        });
    }


    //查询
    function doSearch() {
        //光标所在id
        var idSelector = "";
        var name = ""; //设备名称编码
        var name = $("#name").val();
        if (name != null && name != undefined && name != "") {
            name = name.trim();
        }
        var startDate = $("#actualBeginDate").val();
        var endDate = $("#actualEndDate").val();

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getEquipListByPlan";
        var data = {
            values: {
                equipCodeOrName: name,
                startDate: startDate,
                endDate: endDate,
                ext03: '点检',//1-点检 0-保养
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    list = eval(myobj.list);
                    //构造html
                    creatHtml();

                    //失败
                } else if (myobj.code == "1") {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.toast({
                    msg: JSON.stringify(err),
                    duration: 3000
                });
            }
        });


    }

    function creatHtml() {
        var str = '';
        document.getElementById("tbodyHtml").innerHTML = "";
        for (var i = 0; i < list.length; i++) {
            str += "<div style='margin-top: 10px; height: 120px;color: grey;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);'>";

            str += "    <div style='width: 100%;height: 100%;float: left;font-size:15px;text-align: left;padding-left: 2%;' onclick='goPageDetail(\"" + list[i].code + "\",\"" + list[i].task_code + "\",\"" + list[i].sysid + "\");'>";
            str += "        <div style='font-weight: bold;font-size:15px;color: black;margin-top:10px;'>" + list[i].task_code + "</div>";
            str += "        <div>设备编码：" + list[i].code + "</div>";
            str += "        <div>设备名称：" + list[i].equipmentname + "</div>";
            str += "        <div>设备类型：" + list[i].equipmentTypeName + "</div>";
            // str += "        <div>操作人：" + (list[i].operate_user==null?'':list[i].operate_user) + "</div>";
            str += "        <div>上次点检时间：" + (list[i].create_date == null ? '' : list[i].create_date) + "</div>";
            str += "    </div>";
            str += "</div>";
        }
        $("#tbodyHtml").append(str);
    }


    function test() {
        $api.html($api.byId("tbodyHtml"), "");
    }
    //跳转详情页面
    function goPageDetail(equipcode, taskCode, taskid) {
        //打开页面
        api.openWin({
            name: 'emequipmentPointCheck-detail',
            url: 'emequipmentPointCheck-detail' + '.html',
            pageParam: {
                name: "emequipmentPointCheck",
                taskid: taskid,
                equipcode: equipcode,
                taskCode: taskCode
            },
        });
    }
</script>

</html>